package org.NooLab.demorgraph;


import processing.core.PApplet;

import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.LogControl;


import org.NooLab.randomgraph.RandomGraphFactory;
import org.NooLab.randomgraph.app.IniProperties;
import org.NooLab.randomgraph.app.RGraphPrepare;
import org.NooLab.randomgraph.app.RGraphStartup;


 

/*
 * see:
           http://code.google.com/p/hypergraphdb/wiki/WordNet
           http://code.google.com/p/disko/wiki/InitialHyperGraphDB

 */

/**
 * 
 */
public class RG_Tester_Applet extends PApplet{

	private static final long serialVersionUID = -1206243743183135630L;

	RGraphPrepare rgInstance ;
	 
	PApplet applet;

	
	public void setup(){
		 
		applet = this;
		background( 248,238,148 );
		 
		
		
		try {
			
			RGraphStartup.setApplicationID("rg", this.getClass());
			RGraphPrepare.first();
		
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return;
		}
		  
		showKeyCommands();
		
		// use LogControl to control output of log messages
		LogControl.globeScope = 2; // 1=local definitions  default = 2
		LogControl.Level = 2 ;     // the larger the more detailed logging... (could be really verbose!)
  
		draw();
	}
	
	public void draw(){
		background( 248,238,148);
	}

	private void showKeyCommands(){

		println();
		println("Welcome to Texx Extractor!\n");
		println("the following key commands are available for minimalistic user-based control...");
		println();
		println("   r  ->  start ");
		println();
		println("   d  ->  open another text source ");
		println("   o  ->  open another project ");
		println("   p  ->  select a different base folder (=project space) for all projects ");
		println();
		println("   z  ->  interrupt the current process, export current results and write persistent models ");
		println();
		println("   x  ->  exit");
		println();
		println("------------------------------------------------------------------");
		println();

		_showCurrentInputSettings();
	}
	
	private void _showCurrentInputSettings(){
		String qs="";

		println("current project : "+ RGraphStartup.getLastProjectName()+ ",  data are "+qs+"available.");
		if (qs.length()==0){
			println("data source     : "+ RGraphStartup.getLastDataSet() ) ;
			String dir = DFutils.createPath( IniProperties.fluidSomProjectBasePath, RGraphStartup.getLastProjectName()+"/data/raw/") ;
			
			println("active folder   : "+ dir ) ;
		}

	}
	
	public void keyPressed() {

		 
		if (key=='r'){
			
			boolean rB=false ; 
			looping = false;
			this.noLoop();
			
			if (IniProperties.lastProjectName.length()==0){
				try {
					RGraphStartup.selectActiveProject();
					rB=true;
				} catch (Exception e) {
					rB=false;
				}
			}else{
				rB=true;
			}
			
			
			 
			looping = true;
			this.loop() ;
			
			if (rB){
				startEngines(1) ; // 1= optimizer, 0=simple som (not recommended for most tasks)
			}else{
				println("unable to start the engine.");
			}
		}
		if (key=='d'){
			// open data source
			key=0;
			 
			looping = false;

			String datasrc = RGraphStartup.introduceDataSet() ;
			System.err.println("File selected as source : "+datasrc);
			
			looping = true;
			this.loop() ;
		}
		
		if (key=='o'){
			// open project
			openProject(); 
			// IniProperties behaves very crazy on external jar !!
			// D:\dev\publish\googlecode\iTexx\noodependencies\NooLabUtilities.jar\~nooTxx-ini.properties 
			// (The system cannot find the path specified)

			// it uses the path of the jar file, not of the main class...
		}
		
		if (key=='p'){ //select a different base folder for projects
			 
			looping = false;
			key=0;
			String selectedFolder;
			try {
				selectedFolder = RGraphStartup.selectProjectHome();

			} catch (Exception e) {
				System.err.println(e.getMessage());
				return;
			}
			System.err.println("Folder selected as base folder for projects : "+selectedFolder);
			
			looping = true;
			this.loop() ;
		}
		
		if (key=='P'){ // capital "P": // create project space...
			
			looping = false;
			this.noLoop();
			key=0;
			try {
			
				String psLabel = RGraphStartup.getNewProjectSpaceLabel();
				
				if (RandomGraphFactory.projectSpaceExists(psLabel)){
					return;
				}

				IniProperties.lastProjectName = psLabel ;
				RandomGraphFactory.completeProjectSpace();
			 
				System.err.println("Folder selected as new project space : "+RGraphStartup.getProjectBasePath() + psLabel);
				
				
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
			
			looping = true;
			this.loop() ;
			
		}
		if (key=='x'){
			IniProperties.saveIniProperties();

			System.exit(0);
		}


	}
	
	
	/**
	 * 
	 * @param somtype 0=default: simple, single run; 1=som optimizer
	 * @param lastProjectName2 
	 */
	private void startEngines( int worktype ){
		
		String str="";

		println();
		println("starting project <"+ IniProperties.lastProjectName+">"+str+" ...");
		
		if (worktype>=0){ 
			worktype = RandomGraphFactory._INSTANCE_TYPE_TEXT ;
		}else{
			exit();
		}
		
		
		// the project is equal to the instance: no user-based projects! 
		rgInstance = new RGraphPrepare() ;
		
		rgInstance.defineTestFile( RGraphStartup.getLastDataSet() ) ;
		rgInstance.startTestInstance() ;
	}
	
	private void openProject(){
		
		looping = false;
		this.noLoop(); // that's mandatory, otherwise, the dialog won't be drawn
		
		try {
		
			RGraphStartup.selectActiveProject();
	
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
		looping = true;
		this.loop() ;
		// http://code.google.com/p/processing/source/checkout
	}

		
}


